package eerbaar.client.internal.ctr.pet;

import eerbaar.client.ctr.IPersonaCmd;
import eerbaar.client.exp.IEvaluator;
import eerbaar.client.exp.IExp;
import eerbaar.client.exp.IExpService;
import eerbaar.client.exp.TokenIterator;
import eerbaar.client.internal.ctr.IMultiStimuliHandler;
import eerbaar.client.internal.ctr.cmd.PersonaCmdFactory;
import eerbaar.client.internal.ctr.noun.NounFactory;
import eerbaar.client.internal.ctr.operator.OperatorFactory;
import eerbaar.shared.service.ServiceFactory;

public class MultiPersonaStimuliHandler implements IMultiStimuliHandler {

    public MultiPersonaStimuliHandler() {
        //
    }

    @Override
    public IPersonaCmd command(String name) {
        assert (name != null);

        IPersonaCmd result = null;

        result = PersonaCmdFactory.getCmd(name);
        if (result == null) {

            if (name.startsWith(":")) {
                return null;
            }

            OperatorFactory opFactory = new OperatorFactory();
            NounFactory nounFactory = new NounFactory();

            IExpService expService = ServiceFactory
                    .getService(IExpService.class);

            IEvaluator eval = expService.evaluator(opFactory, nounFactory);
            TokenIterator iterator = expService.scan(name);
            IExp e = eval.parse(iterator);
            if (e != null) {
                result = new EvaluationAdaptor(e);
            }
        }

        return result;
    }

    @Override
    public void input(String input) {
        //
    }

}
